﻿using System;
using System.Collections.Generic;

using Stateless;
using Fuse8.DomainFramework.Domain;

using Fuse8.ContentManagementFramework.SystemFacade.Workflow;
using Fuse8.ContentManagementFramework.Domain.Bases;

namespace Fuse8.ContentManagementFramework.BizFacade.Workflow
{
    public class BaseApprovalWorkflowService<TEntity, TKey> : CmsWorkflowStateMachine<TEntity, TKey>
        where TEntity : class, IEntityObject<TKey>
        where TKey : IComparable<TKey>, IEquatable<TKey>
    {
        #region Overrides

        public override CmsWorkflowState InitialWorkflowState
        {
            get
            {
                return new CmsWorkflowInitialState();
            }
        }

        public override IEnumerable<CmsWorkflowState> AllWorkflowStates
        {
            get 
            {
                var result = new List<CmsWorkflowState>();

                result.Add(new CmsWorkflowInitialState());
                result.Add(new CmsWorkflowAssignedState());
                result.Add(new CmsWorkflowApprovedState());

                return result;
            }
        }

        protected override void Init(StateMachine<CmsWorkflowState, CmsWorkflowStateTransition> stateMachine)
        {
            stateMachine
                .Configure(new CmsWorkflowInitialState())
                .Permit(new CmsSendForApprovalWorkflowStateTransition(), new CmsWorkflowAssignedState());

            stateMachine
                .Configure(new CmsWorkflowAssignedState())
                .Permit(new CmsApproveWorkflowStateTransition(), new CmsWorkflowApprovedState());

            stateMachine
                .Configure(new CmsWorkflowAssignedState())
                .Permit(new CmsRejectWorkflowStateTransition(), new CmsWorkflowInitialState());
        }

        #endregion
    }
}
